﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Globalization;
public partial class Main_Reports_Inventory_OpeningBalanceReport : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    DataTable DT = new DataTable();
    int TotalOpeningBalanceQty = 0;
    double TotalOpeningBalanceValue = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!IsPostBack)
        //{
        //    GetData();
        //}
    }
    protected void GetData()
    {
        if (txtfromdate.Text != "" && txttodate.Text != "")
        {
            lblperiod.Text = "Period :" + txtfromdate.Text + " to " + txttodate.Text;
            SqlParameter[] Param = { new SqlParameter("@fromdate",Convert.ToDateTime(txtfromdate.Text).ToString(string.Format("yyyy/MM/dd"))),
                                   new SqlParameter("@todate",Convert.ToDateTime(txttodate.Text).ToString(string.Format("yyyy/MM/dd")))
                               };
            DT = DU.getdatausingprocedure("IVR_RPT_OPENINGBALANCE_REPORT", Param);
            if (DT.Rows.Count > 0)
            {
                grdViewOPBal.DataSource = DT;
                grdViewOPBal.DataBind();
                lblRecordNo.Text = grdViewOPBal.Rows.Count.ToString() + " Record(s)";
                lblperiod.Text = "Period :" + txtfromdate.Text + " to " + txttodate.Text;
            }
        }
    }
    protected void grdStockReport_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdViewOPBal.PageIndex = e.NewPageIndex;
        GetData();
    }
    protected void btngetreport_Click(object sender, EventArgs e)
    {
        GetData();
    }
    protected void btnReport_Click(object sender, EventArgs e)
    {
        grdViewOPBal.Visible = true;
        GetData();
        string Filter = "";
        if (ddlColumnname.SelectedValue == "1")//for Item Code
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "Itemcode like'%" + txtValue.Text + "%'");
            grdViewOPBal.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "2")//for Item name
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "Itemname Like'%" + txtValue.Text + "%'");
            grdViewOPBal.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "3")//For Project
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "Project Like'%" + txtValue.Text + "%'");
            grdViewOPBal.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "4")//For BinNo
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "BinRef Like'%" + txtValue.Text + "%'");
            grdViewOPBal.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "5")//For MPN
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "AltItemCode Like'%" + txtValue.Text + "%'");
            grdViewOPBal.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "6")//Item Group
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "MPN='" + txtValue.Text + "'");
            grdViewOPBal.AllowPaging = false;
        }
        else if (ddlColumnname.SelectedValue == "7") //For the Inventory Class
        {
            Filter = string.Format(CultureInfo.InvariantCulture, "InventoryClass='" + txtValue.Text + "'");
            grdViewOPBal.AllowPaging = false;
        }
        else
        {
            GetData();
        }
        DataRow[] Dr = DT.Select(Filter);
        DataTable Tempdt = new DataTable();
        Tempdt = DT.Clone();
        foreach (DataRow drNew in Dr)
        {
            Tempdt.ImportRow(drNew);
        }
        grdViewOPBal.DataSource = Tempdt;
        grdViewOPBal.DataBind();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }
    protected void ImgBtnExcel_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "OpeningBalance_Report.xls"));
            Response.ContentType = "application/ms-excel";
            grdViewOPBal.ShowFooter = false;
            grdViewOPBal.AllowPaging = false;
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            grdViewOPBal.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
        }
        catch (Exception ex)
        {
        }
    }
    protected void grdViewOPBal_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{            
        //    TotalOpeningBalanceQty += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "OpeningBalQty"));
        //    TotalOpeningBalanceValue += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "OpeningBalValue"));            
        //}
        //if (e.Row.RowType == DataControlRowType.Footer)
        //{
        //    Label lblTotalQty = (Label)e.Row.FindControl("lblopenQty");
        //    lblTotalQty.Text = TotalOpeningBalanceQty.ToString();
        //    Label lblTotalValue = (Label)e.Row.FindControl("lblopenvalue");
        //    lblTotalValue.Text = TotalOpeningBalanceValue.ToString();          
        //}
    }
}